/*
 * Copyright 2008-2012 Centro Andaluz de Innovación y Tecnologías de la
 * Información y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnológico de Andalucía 29590 - Campanillas, Málaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.youtube.testhelp;

import java.util.List;
import java.util.Random;

import es.citic.puedes.youtube.IYouTubeManager;
import es.citic.puedes.youtube.YouTubeAuthException;
import es.citic.puedes.youtube.YouTubeException;
import es.citic.puedes.youtube.model.YouTubePlayList;
import es.citic.puedes.youtube.model.YouTubePlayListEntry;
import es.citic.puedes.youtube.model.YouTubeUser;
import es.citic.puedes.youtube.test.YouTubeManagerTest;

public class YouTubePlayListsTest {

	public static void runYouTubeGetPlayListTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// gets user profile
		YouTubeUser user = ytm.getUserProfile(userId);

		// gets playlists from user
		List<YouTubePlayList> listOfPlaylists = ytm.getPlayLists(user);
		YouTubePrinterTestMethods.printModelPlayLists(listOfPlaylists);
	}

	public static void runYouTubeAddVideoToPlayListTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		String title, description, playListID;
		
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}
 
		// gets user profile			
		YouTubeUser user = ytm.getUserProfile(userId);
		
		try {
			// get a playlistID from a new playlist
			title = Math.abs((new Random()).nextLong()) + "_A title for a new playlist";
			description = "A description for a new playlist";
			playListID = ytm.addPlayList(user, title, description);
			System.out.println("Id of the new playlist created: " + playListID);
		} catch (Exception e) {
			// get a playlistID from a previous playlist
			playListID = ytm.getPlayLists(user).get(0).getPlaylistId();
			System.out.println("Id of the new playlist created: " + playListID);
		}		

		// adds a video to a playlist
		ytm.addVideoPlayList(user, videoID, playListID);
	}

	public static void runYouTubeDeleteVideoFromPlayListsTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		String title, description, playListID;

		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// gets user profile			
		YouTubeUser user = ytm.getUserProfile(userId);

		try {
			// get a playlistID from a new playlist
			title = Math.abs((new Random()).nextLong()) + "_A title for a new playlist";
			description = "A description for a new playlist";
			playListID = ytm.addPlayList(user, title, description);
			System.out.println("Id of the new playlist created(1): " + playListID);
		} catch (Exception e) {
			// get a playlistID from a previous playlist
			playListID = ytm.getPlayLists(user).get(0).getPlaylistId();
			System.out.println("Id of the new playlist created(2): " + playListID);
		}	
		
		try {
			// adds a video to a playlist
			ytm.addVideoPlayList(user, videoID, playListID);
		} catch (Exception e) {
			// Nothing to do
		}
		
		List<YouTubePlayListEntry> playlistVideoIDList = ytm.getPlayList(playListID);
				
		// deletes a video from a playlist
		ytm.deleteVideoPlayList(user, playlistVideoIDList.get(0).getVideoId(), playListID);
	}

	public static void runYouTubeAddPlayListTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		String title, description, playListID;

		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// gets user profile
		YouTubeUser user = ytm.getUserProfile(userId);

		try {
			// get a playlistID from a new playlist
			title = Math.abs((new Random()).nextLong()) + "_A title for a new playlist";
			description = "A description for a new playlist";
			playListID = ytm.addPlayList(user, title, description);
			System.out.println("Id of the new playlist created: " + playListID);
		} catch (Exception e) {
			// Nothing to do
		}	
	}

	public static void runYouTubeDeletePlayListTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		String title, description, playListID;

		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		// gets user profile			
		YouTubeUser user = ytm.getUserProfile(userId);

		try {
			// get a playlistID from a new playlist
			title = Math.abs((new Random()).nextLong()) + "_A title for a new playlist";
			description = "A description for a new playlist";
			playListID = ytm.addPlayList(user, title, description);
			System.out.println("Id of the new playlist created: " + playListID);
		} catch (Exception e) {
			// get a playlistID from a previous playlist
			playListID = ytm.getPlayLists(user).get(0).getPlaylistId();
			System.out.println("Id of the new playlist created: " + playListID);
		}	
		
		// deletes a playlist
		ytm.deletePlayList(user, playListID);
	}

	public static void runYouTubeGetPlayListPaginationTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}
		
		// gets user profile
		YouTubeUser user = ytm.getUserProfile(userId);

		// gets playlists from user by paging
		List<YouTubePlayList> listOfPlaylists = ytm.getPlayLists(user,1,2);
		YouTubePrinterTestMethods.printModelPlayLists(listOfPlaylists);
	}
}
